<template>
  <el-row class="table-box">
    <el-table
      :size="eltablesize"
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      :empty-text="emptyText"
      @selection-change="handleSelectionChange"
      @cell-click="hbyhby"
       :cell-class-name="statusNum ? cell : ''">
      <el-table-column v-if="selection"
        type="selection"
        width="40">
      </el-table-column>
      <el-table-column v-if="serial"
        type="index"
        label="序号"
        align="center"
        width="65">
      </el-table-column>
      <el-table-column v-for="(item, index) in thead" :key="index"
        :prop="item.prop"
        :label="item.name"
        :show-overflow-tooltip="item.showOverflow"
        :formatter="item.format"
        :width="item.width"
        :align="item.align || allAlign">
      </el-table-column>
      <el-table-column v-if="imageHead" :label="imageHead" align="center" :width="imgwidth">
        <template slot-scope="scope">
          <slot :data="scope.row"></slot>
        </template>
      </el-table-column>
      <el-table-column v-for="(item, index) in lastCol" :label="item.label" align="center" :width="item.width" :key="index">
        <template slot-scope="scope">
          <slot :data="scope.row" :name="item.slotName"></slot>
        </template>
      </el-table-column>
      <el-table-column label="操作" v-if="options.length" :width="optWidth" :align="options.align || allAlign">
        <template slot-scope="scope">
          <template v-for="(item, index) in options">
            <el-button v-if="item.type!=='merge'" @click="item.callback(scope.row, scope.$index)" v-resetClick type="text" size="small" :key="index">{{item.name}}</el-button>
            <el-popover :key="index" v-else placement="top" width="60" v-model="scope.row.visible">
              <div class="mergeDiv">
                <el-button type="text" size="small"
                  v-for="(mergeitem, index) in item.callbacks"
                  @click="mergeitem.callback(scope.row, scope.$index)"
                  :key="index" v-resetClick>
                  {{mergeitem.name}}
                </el-button>
              </div>
              <el-button class="merge-btn addbuttonbtn" slot="reference" type="text" size="small"><i class="el-icon-s-operation" style="font-size:16px;line-height: 1;"></i></el-button>
            </el-popover>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <slot name="footer"></slot>
    <paginate v-if="pageData.callback" :callback="pageData.callback" :page="pageData.page"></paginate>
  </el-row>
</template>
<script>
export default {
  props: {
    eltablesize: { // Table 的尺寸
      type: String,
      default: 'medium'
    },
    tableData: { // 表单数据
      type: Array,
      default: function () {
        return []
      },
      required: true
    },
    thead: { // 表单头信息（prop:对应内容字段， name：标题， width：表格宽度, format：数据格式化, align：文字对齐方式）
      type: Array,
      default: function () {
        return []
      }
    },
    lastCol: { // 最后一列 （prop:对应内容字段， label：标题， width：表格宽度，slotName: 插槽名）
      type: Array,
      default: function () {
        return []
      }
    },
    imageHead: String, // 头像
    serial: { // 序号
      type: Boolean,
      default: false
    },
    selection: { // 复选框
      type: Boolean,
      default: false
    },
    statusNum: {
      type: Number
    },
    options: { // 操作选项
      type: Array,
      default: function () {
        return []
      }
    },
    allAlign: String, // 全部对齐方式
    optWidth: { // 操作宽度
      type: Number
    },
    imgwidth: {
      type: Number
    },
    pageData: { // 分页数据
      type: Object,
      default: function () {
        return {}
      }
    },
    isLoading: { // 是否显示“加载中” 不传则默认为“暂无数据”
      type: Boolean,
      default: false
    }
  },
  computed: {
    emptyText () {
      return this.isLoading ? '数据加载中' : '暂无数据'
    }
  },
  methods: {
    handleSelectionChange (val) { // 选中数据（有复选框时）
      this.$emit('selectData', val)
    },
    hbyhby (row, column, cell, event) {
      this.$emit('hhhh', row, column, cell, event)
    },
    cell ({row, column, rowIndex, columnIndex}) {
      if (columnIndex === Number(this.statusNum)) {
        if (row.MerchantApplet) {
          if (row.MerchantApplet.status === 0) {
            return 'audit auditsuc'
          }
          if (row.MerchantApplet.status === 1) {
            return 'audit auditerr'
          }
          if (row.MerchantApplet.status === 2) {
            return 'audit auditing'
          }
          if (row.MerchantApplet.status === 3) {
            return 'audit unaudit'
          }
        } else if (row.merchantApplet) {
          if (row.merchantApplet.status === 0) {
            return 'audit auditsuc'
          }
          if (row.merchantApplet.status === 1) {
            return 'audit auditerr'
          }
          if (row.merchantApplet.status === 2) {
            return 'audit auditing'
          }
          if (row.merchantApplet.status === 3) {
            return 'audit unaudit'
          }
        } else {
          return 'audit auditerr'
        }
      }
    }
  }
}
</script>
<style>
.el-popover{
  min-width: 70px!important;
  padding: 5px;
}
.addbuttonbtn{
  margin-left: 12px;
  width: 18px;
  color: #66b1ff;
}
</style>
<style lang="less" scoped>
.data-t {
  font-size: 14px;
}
.table-box {
  margin-top: 10px;
}
.mergeDiv{
  width: 100%;
  text-align: center;
  .el-button{
    margin: 0;
  }
}
.merge-btn {
  margin-left: 12px;
}
.show{
  color: red !important;
  font-weight: 700;
}
</style>
